<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            /* background-color: pink; */
            transition: all 2s;
        }

        .box {
            color: white;
            border: none;
            outline: none;
            background-color: black;
            width: 80px;
            height: 40px;
            border-radius: 20px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            transition: all 2s;

        }
    </style>
</head>

<body>
    <button class="box">
        <p>切换黑夜</p>
    </button>

    <script>
        let but = document.querySelector('.box')
        let content = document.querySelector('p')
        let col = document.querySelector('body')

        const Theme = localStorage.getItem('theme');

        if (Theme === 'night') {
            to_night()
        }
        else {
            to_light()
        }

        but.addEventListener('click', function () {
            if (content.innerText === '切换白天') {
                to_night()
                localStorage.setItem('theme', 'night')
            }
            else {
                to_light()
                localStorage.setItem('theme', 'light')
            }
        })

        function to_light() {
            col.style.backgroundColor = 'black'
            content.innerText = '切换白天'
            but.style.backgroundColor = 'white'
            content.style.color = 'black'
        }

        function to_night() {
            col.style.backgroundColor = 'white'
            content.innerText = '切换黑夜'
            but.style.backgroundColor = 'black'
            content.style.color = 'white'
        }
    </script>
</body>

</html>